<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,user-scalable=no" />
		<title></title>
		<style>
			body{
				margin: 0;
			}
			body,html{
				height: 100%;
				overflow: hidden;
				position: relative;
			}
			header,footer{
				height: 40px;
				font-size: 20px;
				text-align: center;
				background: #000;
				line-height: 40px;
				color: #fff;
			}
			footer{
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
			}
			#wrap{
				position: absolute;
				left: 0;
				right:0;
				top: 40px;
				bottom: 40px;
				overflow: hidden;
			}
			#list{
				list-style: none;
				margin: 0;
				padding: 0;
				
			}
			#list li{
				border-bottom:1px solid #000 ;
				height: 35px;
				line-height: 35px;
				text-indent: 2em;
			}
		</style>
		<script src="js/m.Tween.js"></script>
		<script>
			function creatList(){
				var list = document.querySelector("#list");
				var inner = "";
				for(var i = 0; i < 100; i++){
					inner += "<li>这是第"+ i +"个li</li>";
				};
				list.innerHTML = inner;
			}
			document.addEventListener("touchstart",function(e){
				e.preventDefault();
			})
			window.onload = function(){
				creatList();
				var wrap = document.querySelector("#wrap");
				var list = document.querySelector("#list");
				var scrollBar = document.createElement("div");
				var startPoint = 0; //手指按下去的坐标
				var startEl = 0;    //元素当手指按下时的位置
				var lastDis = 0;	//元素最后一次移动的距离
				var lastY = 0;		//元素最后一次所在的位置
				var lastTime = 0;
				var lastTimeDis = 0;
				var maxTranslate = wrap.clientHeight - list.offsetHeight;
				var scale = wrap.clientHeight/list.offsetHeight;
				list.style.minHeight = "100%";
				scrollBar.style.cssText = "width: 6px;background:rgba(0,0,0,.5) ;position: absolute;right:0;top: 0;border-radius: 3px; opacity: 0;transition: .3s opacity;";
				wrap.appendChild(scrollBar);
				css(list,"translateZ",0.01);
				list.addEventListener("touchstart",function(e){
					maxTranslate = wrap.clientHeight - list.offsetHeight;
					if( maxTranslate >= 0 ){
						scrollBar.style.display = "none";
					}else{
						scrollBar.style.display = "block";
					}
					scale = wrap.clientHeight/list.offsetHeight;
					scrollBar.style.height = wrap.clientHeight*scale+"px";
					clearInterval(list.timer);
					startPoint = e.changedTouches[0].pageY;
					startEl = css(list,'translateY');
					lastY = startEl;
					lastDis = 0;
					lastTime = new Date().getTime();
					lastTimeDis = 0;
					scrollBar.style.opacity = 1;
					
				});
				list.addEventListener("touchmove",function(e){
					var nowTime = new Date().getTime();
					var nowPoint = e.changedTouches[0].pageY; 
					var dis = nowPoint-startPoint;
					var translateY = dis + startEl;
					css(list,'translateY',translateY);
					css(scrollBar,'translateY',-translateY*scale);
					lastDis = translateY - lastY;
					lastY = translateY;
					lastTimeDis = nowTime - lastTime;
					lastTime = nowTime;
				});
				list.addEventListener('touchend',function(e){
					var type = "easeOut";
					var speed = Math.round(lastDis / lastTimeDis*10);
					speed = lastTimeDis <=0 ? 0 : speed;
					var target = Math.round(speed*30 + css(list,'translateY'));
					if( target > 0){
						target = 0;
						type = "backOut";
					}else if( target < maxTranslate ){
						target = maxTranslate;
						type = "backOut";
					};
					MTween({
						el:list,
						target:{translateY:target},
						time:Math.round(Math.abs(target - css(list,'translateY'))*1.8),
						type:"easeOut",
						callBack:function(){
							scrollBar.style.opacity = 0;
						},
						callIn:function(){
							var translateY = css(list,"translateY");
							css(scrollBar,'translateY',-translateY*scale);
						}
					});
					
				})
			}
		</script>
	</head>
	<body>
		<header id="header">妙味课堂</header>
		<section id="wrap">
			<ul id="list"></ul>
		</section>
		<footer id="footer">运用translate进行滑屏操作</footer>
	</body>
</html>
